﻿package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.core.render.data.RenderHitData;
	import org.papervision3d.core.render.data.RenderSessionData;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.shadematerials.PhongMaterial;
	
	import org.cove.ape.*;
	
	import org.papervision3d.core.math.Plane3D;
	import org.papervision3d.core.math.Sphere3D;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.core.utils.Mouse3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.core.geom.Lines3D;
	
	import com.tris.*;
	import com.tris.apevision.shapes.* ;
	//merge test
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class MainTest extends PapervisionBase
	{
		private static var pointLight:PointLight3D;
		private var cube:AVRectoid;
		private var mouse3D:Mouse3D;
		
		public var renderSesh:RenderSessionData;
		
		public function MainTest() 
		{
			initLighting();
			
			renderSesh = new RenderSessionData();
            renderSesh.scene = scene;
			
			mouse3D = new Mouse3D();
			Mouse3D.enabled = true;
			
			stage.frameRate = 60;
			addEventListener(Event.ENTER_FRAME, run);
			stage.addEventListener(MouseEvent.CLICK, onClick);
			
						
			var cubeyPhonga:PhongMaterial = new PhongMaterial(pointLight, 0xffffff, 0x447700, 20);
			var materials:MaterialsList = new MaterialsList( {
				top: cubeyPhonga,
				bottom: cubeyPhonga,
				left: cubeyPhonga,
				right: cubeyPhonga,
				front: cubeyPhonga,
				back: cubeyPhonga
			});
			
			cube = new AVRectoid(scene, materials, 200, 0, 500, 100, 100, 40, true);
			var cube2:AVRectoid = new AVRectoid(scene, materials, -100, 0, 500, 100, 100, 1, true);
			
			defaultGroup.addParticle( cube );
			defaultGroup.addParticle( cube2 );
			
			var cb:PhongMaterial = new PhongMaterial(pointLight, 0xffffff, 0x447700, 20);
			var lineMaterial:LineMaterial = new LineMaterial(0xffaa00);
			addPendulum(cb, lineMaterial, 300, -180, 20, 300, 10, 40, defaultGroup);
			addPendulum(cb, lineMaterial, 530, -300, 20, 500, -50, 40, defaultGroup);
			
		}
		
		private function initLighting():void
		{
			pointLight = new PointLight3D(true);
			pointLight.z = -1500;
			pointLight.y = 100;
			scene.addChild(pointLight);
		}
		
		private function addBall():void
		{
			
			//var phonga:PhongMaterial = new PhongMaterial(pointLight, 0xffffff, 0x00bb00, 20);
			var bitywaa:BitmapFileMaterial = new BitmapFileMaterial("goat5.jpg", true);
			var sphere:AVSpheroid = new AVSpheroid(scene, bitywaa, Math.random()*300, -1000, 50, false, 1, Math.random(), 0.1, 0.1);
			defaultGroup.addParticle( sphere );
			trace("goat!");
		}
		private function addPendulum(mat:MaterialObject3D, lineMat:LineMaterial, pegX:Number, pegY:Number, pegR:Number, plumbX:Number, plumbY:Number, plumbR:Number, group:Group):void
		{
			var peg:AVSpheroid = new AVSpheroid(scene, mat, pegX, pegY, pegR, true);
			var plumb:AVSpheroid = new AVSpheroid(scene, mat, plumbX, plumbY, plumbR);
			var pegPlumb:AVSpringConstraint = new AVSpringConstraint(scene, lineMat, peg, plumb, 5 );
			group.addParticle( peg );
			group.addParticle( plumb);
			group.addConstraint( pegPlumb );
		}
		private function onClick(e:MouseEvent):void
		{
			addBall();
			checkMouse();
		}
		private function checkMouse():void
		{
			//var rhd:RenderHitData = new RenderHitData(viewport.hitTestPoint2D(new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY)));
			//mouse3D.updatePosition(rhd);
			//trace(mouse3D.hitTestObject(scene.getChildByName("cuboid"), 1));
		}
		
		private function run(evt:Event):void
		{
			//cube.px += 1;
			cube.angle += 3;
			APEngine.step();
			APEngine.paint();
		}
	}
	
	
}